<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="售货员ID" prop="sellerId">
            <el-input v-model="queryParams.sellerId" placeholder="请输入售货员ID" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="账号标识" prop="accountKey">
            <el-input v-model="queryParams.accountKey" placeholder="请输入账号标识" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="账号名称" prop="accountName">
            <el-input v-model="queryParams.accountName" placeholder="请输入账号名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="支付方式" prop="paymentWay">
            <el-select v-model="queryParams.paymentWay" placeholder="支付方式" clearable style="width: 240px">
              <el-option v-for="dict in biz_payment_way" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
              <el-option
                v-for="dict in sys_normal_disable"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['retail:sellerCashier:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['retail:sellerCashier:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="sellerCashierList" @selection-change="handleSelectionChange" class="tableEmpty">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="id" v-if="false" />
        <el-table-column label="售货员ID" align="center" prop="sellerId" />
        <el-table-column label="账号类型" align="center" prop="accountType" />
        <el-table-column label="账号标识" align="center" prop="accountKey" />
        <el-table-column label="账号名称" align="center" prop="accountName" />
        <el-table-column label="支付方式" align="center" prop="paymentWay">
          <template #default="scope">
            <dict-tag :options="biz_payment_way" :value="scope.row.paymentWay" />
          </template>
        </el-table-column>        <el-table-column label="支付地址" align="center" prop="paymentLink" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['retail:sellerCashier:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script setup name="SellerCashier" lang="ts">
import { listSellerCashier, delSellerCashier } from '@/api/retail/sellerCashier';
import { SellerCashierVO, SellerCashierQuery, SellerCashierForm } from '@/api/retail/sellerCashier/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_normal_disable,biz_payment_way,biz_account_type } = toRefs<any>(proxy?.useDict('sys_normal_disable','biz_payment_way','biz_account_type'));

const sellerCashierList = ref<SellerCashierVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const sellerCashierFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: SellerCashierForm = {
  id: undefined,
  sellerId: undefined,
  accountType: undefined,
  accountKey: undefined,
  accountName: undefined,
  paymentWay: undefined,
  paymentLink: undefined,
  status: undefined,
}
const data = reactive<PageData<SellerCashierForm, SellerCashierQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sellerId: undefined,
    accountType: undefined,
    accountKey: undefined,
    accountName: undefined,
    paymentWay: undefined,
    paymentLink: undefined,
    status: undefined,
    params: {
    }
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询售货员收银账号列表 */
const getList = async () => {
  loading.value = true;
  const res = await listSellerCashier(queryParams.value);
  sellerCashierList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  sellerCashierFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: SellerCashierVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 删除按钮操作 */
const handleDelete = async (row?: SellerCashierVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除售货员收银账号编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delSellerCashier(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('retail/sellerCashier/export', {
    ...queryParams.value
  }, `sellerCashier_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
